﻿Imports Lucene.Net.Analysis.Standard
Imports Lucene.Net.Documents
Imports Lucene.Net.Store
Imports Directory = Lucene.Net.Store.Directory
Imports Lucene.Net.Index
Imports Lucene.Net.Search
Imports Lucene.Net.QueryParsers

Public Class simplesearch
    Inherits System.Web.UI.Page



    Protected Sub btnSearchNow_Click(sender As Object, e As EventArgs) Handles btnSearchNow.Click
        Dim analyz As New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
        Dim mydir As Directory = FSDirectory.Open(New System.IO.DirectoryInfo(Server.MapPath("\indexfolder")))
        Dim ir As IndexReader = IndexReader.Open(mydir, True)
        Dim isr As Searcher = New IndexSearcher(ir)

        Dim qrp As New QueryParser(Lucene.Net.Util.Version.LUCENE_29, "AdText", analyz)
        Me.ListBox1.Items.Clear()

        Dim qr = qrp.Parse(Me.TextBox1.Text)

        Me.ListBox1.Items.Add("Searching ads for Text : " & Me.TextBox1.Text)

        Dim resultDocs As TopDocs = isr.Search(qr, ir.MaxDoc())
        Dim hits = resultDocs.ScoreDocs
        Me.lblResultsCounter.Text = hits.Count & " results found"
        For Each hit In hits
            Dim docsFromSearch = isr.Doc(hit.Doc)
            Me.ListBox1.Items.Add(docsFromSearch.Get("AdID").ToString & " " & docsFromSearch.Get("AdText").ToString)
        Next
    End Sub
End Class